package com.smfx.yxt.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.ExchangeCourseType;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.common.enums.TeacherChangeCourseApply;
import com.smfx.yxt.daoSqlserver.OrganizeGroupMapper;
import com.smfx.yxt.daoSqlserver.ReqTeacherExchangeCourseApplyMapper;
import com.smfx.yxt.daoSqlserver.TeacherExchangeCourseApplyItemMapper;
import com.smfx.yxt.daoSqlserver.TeacherExchangeCourseApplyMapper;
import com.smfx.yxt.daoSqlserver.TeacherInfoMapper;
import com.smfx.yxt.model.ReqTeacherExchangeCourseApply;
import com.smfx.yxt.model.TeacherExchangeCourseApply;
import com.smfx.yxt.model.TeacherInfo;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.TeacherExchangeCourseApplyItemReqVO;
import com.smfx.yxt.reqVo.TeacherExchangeCourseApplyReqVO;
import com.smfx.yxt.service.ITeacherChangeCourseApplyService;
import com.smfx.yxt.utils.DateUtil;
import com.smfx.yxt.view.TeacherExchangeCourseApplyItemView;
import com.smfx.yxt.view.TeacherExchangeCourseApplyView;
import com.smfx.yxt.wechat.utils.TemplateUtil;

@Service("teacherChangeCourseApplyService")
public class TeacherChangeCourseApplyServiceImpl implements ITeacherChangeCourseApplyService {

	@Autowired
	private TeacherExchangeCourseApplyMapper teacherExchangeCourseApplyMapper;
	@Autowired
	private TeacherExchangeCourseApplyItemMapper teacherExchangeCourseApplyItemMapper;
	@Autowired
	private ReqTeacherExchangeCourseApplyMapper reqTeacherExchangeCourseApplyMapper;
	@Autowired
	private TeacherInfoMapper teacherInfoMapper;
	@Autowired
	private OrganizeGroupMapper organizeGroupMapper;
	
	@Override
	public int insertSelective(TeacherExchangeCourseApply record) {
		return teacherExchangeCourseApplyMapper.insertSelective(record);
	}

	@Override
	public TeacherExchangeCourseApply selectByPrimaryKey(Integer id) {
		return teacherExchangeCourseApplyMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(TeacherExchangeCourseApply record) {
		return teacherExchangeCourseApplyMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public List<TeacherExchangeCourseApplyView> selectPendingListByPage(Page<TeacherExchangeCourseApplyView> page) {
		return teacherExchangeCourseApplyMapper.selectPendingListByPage(page);
	}

	@Override
	public List<TeacherExchangeCourseApplyView> selectRejectListByPage(Page<TeacherExchangeCourseApplyView> page) {
		return teacherExchangeCourseApplyMapper.selectRejectListByPage(page);
	}

	@Override
	public List<TeacherExchangeCourseApplyView> selectAgreeListByPage(Page<TeacherExchangeCourseApplyView> page) {
		return teacherExchangeCourseApplyMapper.selectAgreeListByPage(page);
	}

	@Override
	public TeacherExchangeCourseApplyView selectViewById(Integer id) {
		TeacherExchangeCourseApplyView view = teacherExchangeCourseApplyMapper.selectViewById(id);
		view.setItemList(teacherExchangeCourseApplyItemMapper.selectListByApplyId(view.getId()));
		view.setReqList(reqTeacherExchangeCourseApplyMapper.selectListByApplyId(view.getId()));
		return view;
	}

	@Override
	public int addApply(TeacherExchangeCourseApplyReqVO reqVO, TeacherInfo teacher) {
		teacherExchangeCourseApplyMapper.insertSelective(reqVO);
		ReqTeacherExchangeCourseApply reqApply = new ReqTeacherExchangeCourseApply();
		reqApply.setApplyId(reqVO.getId());
		reqApply.setTeacherId(teacher.getId());
		reqApply.setTeacherName(teacher.getName());
		reqApply.setStatus(TeacherChangeCourseApply.PENDING.getCode());
		reqTeacherExchangeCourseApplyMapper.insertSelective(reqApply);
		for(TeacherExchangeCourseApplyItemReqVO apply : reqVO.getList()) {
			apply.setApplyId(reqVO.getId());
			teacherExchangeCourseApplyItemMapper.insertSelective(apply);
		}
		new Thread(new TeacherExchangeCourseExecutor(reqVO, reqVO.getStatus(), teacher)).start();
		return 1;
	}

	@Override
	public RspResult updateAudit(TeacherExchangeCourseApplyReqVO reqVO, TeacherInfo teacher) {
		TeacherExchangeCourseApply apply = teacherExchangeCourseApplyMapper.selectByPrimaryKey(reqVO.getId());
		if(!apply.getStatus().equals(reqVO.getPrevStatus())) {
			return new RspResult(Status.EXPIRED_DATA);
		}
		reqVO.setUpdateTime(new Date());
		reqVO.setApproverId(teacher.getId());
		reqVO.setApproverName(teacher.getName());
		teacherExchangeCourseApplyMapper.updateByPrimaryKeySelective(reqVO);
		for(TeacherExchangeCourseApplyItemReqVO itemReqVO : reqVO.getList()) {
			itemReqVO.setUpdateTime(new Date());
			itemReqVO.setStatus(TeacherChangeCourseApply.AGREE.getCode());
			teacherExchangeCourseApplyItemMapper.updateByPrimaryKeySelective(itemReqVO);
		}
		ReqTeacherExchangeCourseApply reqApply = new ReqTeacherExchangeCourseApply();
		reqApply.setTeacherId(teacher.getId());
		reqApply.setTeacherName(teacher.getName());
		reqApply.setStatus(reqVO.getStatus());
		reqApply.setRejectReason(reqVO.getRejectReason());
		reqApply.setApplyId(apply.getId());
		reqTeacherExchangeCourseApplyMapper.insertSelective(reqApply);
		TeacherExchangeCourseApply courseApply = teacherExchangeCourseApplyMapper.selectByPrimaryKey(apply.getId());
		new Thread(new TeacherExchangeCourseExecutor(courseApply, reqVO.getStatus(), teacher)).start();
		return new RspResult(Status.SUCCESS);
	}
	
	@Override
	public RspResult updateTeacherAudit(TeacherExchangeCourseApplyItemReqVO reqVO, TeacherInfo teacher) {
		reqVO.setUpdateTime(new Date());
		teacherExchangeCourseApplyItemMapper.updateByPrimaryKeySelective(reqVO);
		ReqTeacherExchangeCourseApply reqApply = new ReqTeacherExchangeCourseApply();
		reqApply.setTeacherId(teacher.getId());
		reqApply.setTeacherName(teacher.getName());
		reqApply.setStatus(reqVO.getStatus());
		reqApply.setRejectReason(reqVO.getRejectReason());
		reqApply.setApplyId(reqVO.getApplyId());
		reqApply.setApplyItemId(reqVO.getId());
		reqTeacherExchangeCourseApplyMapper.insertSelective(reqApply);
		TeacherExchangeCourseApply apply = new TeacherExchangeCourseApply();
		apply.setId(reqVO.getApplyId());
		apply.setUpdateTime(new Date());
		if(reqVO.getStatus().equals(TeacherChangeCourseApply.EXCHANGE_TEACHER_NO.getCode())) {
			apply.setStatus(reqVO.getStatus());
			teacherExchangeCourseApplyMapper.updateByPrimaryKeySelective(apply);
			TeacherExchangeCourseApply courseApply = teacherExchangeCourseApplyMapper.selectByPrimaryKey(reqVO.getApplyId());
			new Thread(new TeacherExchangeCourseExecutor(courseApply, reqVO.getStatus(), teacher)).start();
		}else {
			int count = teacherExchangeCourseApplyItemMapper.selectCountForPending(reqVO.getApplyId());
			if(count == 0) {
				apply.setStatus(com.smfx.yxt.common.enums.TeacherChangeCourseApply.AUDIT_TEACHER_ING.getCode());
				teacherExchangeCourseApplyMapper.updateByPrimaryKeySelective(apply);
				TeacherExchangeCourseApply courseApply = teacherExchangeCourseApplyMapper.selectByPrimaryKey(reqVO.getApplyId());
				new Thread(new TeacherExchangeCourseExecutor(courseApply, apply.getStatus(), teacher)).start();
			}
		}
		
		return new RspResult(Status.SUCCESS);
	}

	class TeacherExchangeCourseExecutor implements Runnable {

		private TeacherExchangeCourseApply apply;
		
		private String reqStatus;
		
		private TeacherInfo approvalTeacher;
		
		public TeacherExchangeCourseExecutor(TeacherExchangeCourseApply apply, 
				String reqStatus, TeacherInfo approvalTeacher) {
			this.apply = apply;
			this.reqStatus = reqStatus;
			this.approvalTeacher = approvalTeacher;
		}
		
		@Override
		public void run() {
			try {
				String reqUrl = Const.BASEPATH + Const.TEACHER_ROLE + MenuConst.TEACHER_CHANGE_COURSE + "/detail/" + apply.getId();
				if(reqStatus.equals(TeacherChangeCourseApply.PENDING.getCode())){
					List<TeacherExchangeCourseApplyItemView> list = teacherExchangeCourseApplyItemMapper.selectExchangeTeacherListByApplyId(apply.getId());
					for(TeacherExchangeCourseApplyItemView record : list) {
						String currDateLesson = DateUtil.dateToStr(record.getCourseDate(), DateUtil.DEFAULT_DATE_FORMAT3) + record.getLesson();
						String exchangeDateLesson = record.getOptionType().equals(ExchangeCourseType.CHANGE.getCode()) 
								? DateUtil.dateToStr(record.getExchangeCourseDate(), DateUtil.DEFAULT_DATE_FORMAT3) + record.getExchangeLesson() : "";
						reqUrl = Const.BASEPATH + Const.TEACHER_ROLE + MenuConst.TEACHER_CHANGE_COURSE + "/itemDetail/" + record.getId();
						TemplateUtil.changeCourseSend(record.getOpenid(), record.getExchangeTeacherName(), 
								apply.getApplyTeacherName(), record.getGradeName() + record.getClazz() + "班" + record.getSubjectName(), 
								currDateLesson, exchangeDateLesson, reqUrl);
					}
			    }else if(reqStatus.equals(TeacherChangeCourseApply.EXCHANGE_TEACHER_NO.getCode())) {
					String msg = "亲爱的" + apply.getApplyTeacherName() + "老师,您的调代看课申请已被" + approvalTeacher.getName() + "老师驳回，请知悉";
					TeacherInfo teacher = teacherInfoMapper.selectByPrimaryKey(apply.getApplyTeacherId());
					TemplateUtil.auditResultSend(teacher.getOpenid2(), approvalTeacher.getName(),
							com.smfx.yxt.common.enums.TeacherChangeCourseApply.getValue(reqStatus), apply.getUpdateTime(), 
							msg, reqUrl);
				}else if (reqStatus.equals(TeacherChangeCourseApply.AUDIT_TEACHER_ING.getCode())) {
					List<TeacherInfo> list = organizeGroupMapper.selectCourseApprover();
					for(TeacherInfo teacherInfo : list) {
						TemplateUtil.auditChangeCourseSend(teacherInfo.getOpenid2(), teacherInfo.getName(), 
								apply.getApplyTeacherName(), apply.getReason(), reqUrl);
					}
				}else if(reqStatus.equals(TeacherChangeCourseApply.AUDIT_TEACHER_NO.getCode())){
					List<String> ids = new ArrayList<>();
					ids.add(String.valueOf(apply.getApplyTeacherId()));
					ids.addAll(teacherExchangeCourseApplyItemMapper.selectExchangeTeacherIdByApplyId(apply.getId()));
					List<TeacherInfo> list = teacherInfoMapper.selectListByIds(ids);
					for(TeacherInfo teacher : list) {
						String msg = apply.getApplyTeacherName()+"老师的调代看课申请已被教务处驳回，请知悉";
						TemplateUtil.auditResultSend(teacher.getOpenid2(), approvalTeacher.getName(), 
								com.smfx.yxt.common.enums.TeacherChangeCourseApply.getValue(reqStatus), apply.getUpdateTime(), 
								msg, reqUrl);
					}
				}else if(reqStatus.equals(TeacherChangeCourseApply.AGREE.getCode()) || 
						reqStatus.equals(TeacherChangeCourseApply.AUDIT_TEACHER_CANCEL.getCode())) {
					List<String> ids = new ArrayList<>();
					ids.add(String.valueOf(apply.getApplyTeacherId()));
					ids.addAll(teacherExchangeCourseApplyItemMapper.selectExchangeTeacherIdByApplyId(apply.getId()));
					List<TeacherInfo> list = teacherInfoMapper.selectListByIds(ids);
					String statusText = com.smfx.yxt.common.enums.TeacherChangeCourseApply.getValue(reqStatus);
					for(TeacherInfo teacher : list) {
						String msg = apply.getApplyTeacherName()+"教师的调代看课申请已被"+statusText+"，请合理安排时间";
						TemplateUtil.auditResultSend(teacher.getOpenid2(), approvalTeacher.getName(), 
								statusText, apply.getUpdateTime(), msg, reqUrl);
					}
					list = organizeGroupMapper.selectSubjectGradeTeacher();
					for(TeacherInfo teacher : list) {
						String msg = apply.getApplyTeacherName()+"教师的调代看课申请已被"+statusText+"，请各位级长、科长知悉";
						TemplateUtil.auditResultSend(teacher.getOpenid2(), approvalTeacher.getName(), 
								statusText, apply.getUpdateTime(), msg, reqUrl);
					}
				}
				/*if(apply.getStatus().equals(com.smfx.yxt.common.enums.TeacherChangeCourseApply.PENDING.getCode())){
					TeacherInfo teacherInfo = teacherInfoMapper.selectByPrimaryKey(reqVO.getExchangeTeacherId());
					TemplateUtil.changeCourseSend(teacherInfo.getOpenid2(), teacherInfo.getName(), 
							apply.getApplyTeacherName(), apply.getSubjectName(), reqVO.getCourseDateStr()+reqVO.getCurrLesson(), 
							reqVO.getExchangeCourseDateStr()+reqVO.getExchangeLesson(), reqUrl);
				}else if(reqVO.getStatus().equals(com.smfx.yxt.common.enums.TeacherChangeCourseApply.EXCHANGE_TEACHER_OK.getCode())){
					List<TeacherInfo> list = organizeGroupMapper.selectCourseApprover();
					for(TeacherInfo teacherInfo : list) {
						TemplateUtil.changeCourseSend(teacherInfo.getOpenid2(), teacherInfo.getName(), 
								record.getApplyTeacherName(), record.getSubjectName(), DateUtil.dateToStr(record.getCourseDate(),DateUtil.DEFAULT_DATE_FORMAT3)+record.getCurrLesson(), 
								DateUtil.dateToStr(record.getExchangeCourseDate(),DateUtil.DEFAULT_DATE_FORMAT3)+record.getExchangeLesson(), reqUrl);
					}
				}else if(reqVO.getStatus().equals(com.smfx.yxt.common.enums.TeacherChangeCourseApply.AGREE.getCode())) {
					List<String> ids = new ArrayList<>();
					ids.add(String.valueOf(record.getApplyTeacherId()));
					ids.add(String.valueOf(record.getExchangeTeacherId()));
					List<TeacherInfo> list = teacherInfoMapper.selectListByIds(ids);
					for(TeacherInfo teacher : list) {
						String msg = record.getApplyTeacherName()+"教师的调课申请已审批成功，请合理安排时间";
						TemplateUtil.auditResultSend(teacher.getOpenid2(), approvalTeacher.getName(), 
								com.smfx.yxt.common.enums.TeacherChangeCourseApply.getValue(record.getStatus()), reqVO.getUpdateTime(), 
								msg, reqUrl);
					}
					list = organizeGroupMapper.selectSubjectGradeTeacher();
					for(TeacherInfo teacher : list) {
						String msg = record.getApplyTeacherName()+"教师的调课申请已审批成功，请各位级长、科长知悉";
						TemplateUtil.auditResultSend(teacher.getOpenid2(), approvalTeacher.getName(), 
								com.smfx.yxt.common.enums.TeacherChangeCourseApply.getValue(record.getStatus()), reqVO.getUpdateTime(), 
								msg, reqUrl);
					}
				}else {
					if(reqVO.getStatus().equals(com.smfx.yxt.common.enums.TeacherChangeCourseApply.EXCHANGE_TEACHER_NO.getCode())){
						String msg = "亲爱的"+record.getApplyTeacherName()+"老师,您的调课申请已被"+record.getExchangeTeacherName()+"老师驳回，请知悉";
						TeacherInfo teacher = teacherInfoMapper.selectByPrimaryKey(record.getApplyTeacherId());
						TemplateUtil.auditResultSend(teacher.getOpenid2(), approvalTeacher.getName(), 
								com.smfx.yxt.common.enums.TeacherChangeCourseApply.getValue(record.getStatus()), reqVO.getUpdateTime(), 
								msg, reqUrl);
					}else if (reqVO.getStatus().equals(com.smfx.yxt.common.enums.TeacherChangeCourseApply.AUDIT_TEACHER_NO.getCode())) {
						List<String> ids = new ArrayList<>();
						ids.add(String.valueOf(record.getApplyTeacherId()));
						ids.add(String.valueOf(record.getExchangeTeacherId()));
						List<TeacherInfo> list = teacherInfoMapper.selectListByIds(ids);
						for(TeacherInfo teacher : list) {
							String msg = record.getApplyTeacherName()+"老师的调课申请已被教务处驳回，请知悉";
							TemplateUtil.auditResultSend(teacher.getOpenid2(), approvalTeacher.getName(), 
									com.smfx.yxt.common.enums.TeacherChangeCourseApply.getValue(record.getStatus()), reqVO.getUpdateTime(), 
									msg, reqUrl);
						}
					}else if(reqVO.getStatus().equals(com.smfx.yxt.common.enums.TeacherChangeCourseApply.AUDIT_TEACHER_CANCEL.getCode())) {
						List<String> ids = new ArrayList<>();
						ids.add(String.valueOf(record.getApplyTeacherId()));
						ids.add(String.valueOf(record.getExchangeTeacherId()));
						List<TeacherInfo> list = teacherInfoMapper.selectListByIds(ids);
						for(TeacherInfo teacher : list) {
							String msg = record.getApplyTeacherName()+"老师的调课申请已被教务处撤销，请知悉";
							TemplateUtil.auditResultSend(teacher.getOpenid2(), approvalTeacher.getName(), 
									com.smfx.yxt.common.enums.TeacherChangeCourseApply.getValue(record.getStatus()), reqVO.getUpdateTime(), 
									msg, reqUrl);
						}
						list = organizeGroupMapper.selectSubjectGradeTeacher();
						for(TeacherInfo teacher : list) {
							String msg = record.getApplyTeacherName()+"教师的调课申请已被教务处撤销，请各位级长、科长知悉";
							TemplateUtil.auditResultSend(teacher.getOpenid2(), approvalTeacher.getName(), 
									com.smfx.yxt.common.enums.TeacherChangeCourseApply.getValue(record.getStatus()), reqVO.getUpdateTime(), 
									msg, reqUrl);
						}
					}
				}*/
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}

	@Override
	public List<TeacherExchangeCourseApplyView> selectListByPage(Page<TeacherExchangeCourseApplyView> page) {
		return teacherExchangeCourseApplyMapper.selectListByPage(page);
	}

	@Override
	public List<TeacherExchangeCourseApplyItemView> selectItemListByPage(
			Page<TeacherExchangeCourseApplyItemView> page) {
		return teacherExchangeCourseApplyItemMapper.selectItemListByPage(page);
	}

	@Override
	public List<TeacherExchangeCourseApplyItemView> selectListByApplyId(Integer applyId) {
		return teacherExchangeCourseApplyItemMapper.selectListByApplyId(applyId);
	}

	@Override
	public TeacherExchangeCourseApplyItemView selectByItemId(Integer itemId) {
		return teacherExchangeCourseApplyItemMapper.selectByItemId(itemId);
	}

	@Override
	public List<TeacherExchangeCourseApplyItemView> selectReportListByPage(
			Page<TeacherExchangeCourseApplyItemView> page) {
		return teacherExchangeCourseApplyItemMapper.selectReportListByPage(page);
	}

}
